#include "init_peripheral.h"

/**
 * 初始化选通器
 */
void init_power(void)
{
	pinMode(OUTPUT_3V3_PIN, OUTPUT);
	digitalWrite(OUTPUT_3V3_PIN, HIGH);

	pinMode(OUTPUT_1V8_PIN, OUTPUT);
	digitalWrite(OUTPUT_1V8_PIN, HIGH);

	pinMode(OUTPUT_5V_PIN, OUTPUT);
	digitalWrite(OUTPUT_5V_PIN, HIGH);
	delay(100);
}

void deinit_power(void)
{
	pinMode(OUTPUT_3V3_PIN, OUTPUT);
	digitalWrite(OUTPUT_3V3_PIN, LOW);

	pinMode(OUTPUT_1V8_PIN, OUTPUT);
	digitalWrite(OUTPUT_1V8_PIN, LOW);

	pinMode(OUTPUT_5V_PIN, OUTPUT);
	digitalWrite(OUTPUT_5V_PIN, LOW);
	delay(100);
}

MAX30003 max30003;
/**
 * 初始化MAX30003
 */
void max30003_Init(void)
{
	pinMode(SS, OUTPUT);
	digitalWrite(SS, HIGH); // disable device

	// SPI.begin(18, 19, 23, 5); //VSPI
	SPI.begin();
	SPI.setBitOrder(MSBFIRST);
	SPI.setDataMode(SPI_MODE0);

	bool ret = max30003.max30003ReadInfo();
	if (ret)
	{
		Serial.println("Max30003 read ID Success");
	}
	else
	{
		while (!ret)
		{
			// stay here untill the issue is fixed.
			ret = max30003.max30003ReadInfo();
			Serial.println("Failed to read ID, please make sure all the pins are connected");
			delay(10000);
		}
	}
	Serial.println("Initialising the chip ...");
	max30003.max30003Begin(); 
}

FDC2214 capsense(FDC2214_I2C_ADDR_0); // Use FDC2214_I2C_ADDR_1 
/**
 * 初始化FDC2214
 */
void fdc2214_init(void)
{
    // ESP32有两个Wire，需要释放掉其中一个Wire用来连接FDC2214
	Wire_FDC2214.begin(FDC2214_SDA_PIN, FDC2214_SCL_PIN, 100000);
  	// Start FDC2212 with 2 channels init
  	bool capOk = capsense.begin(0x1, 0x4, 0x5); //setup first two channels, autoscan with 2 channels, deglitch at 10MHz 
  	// Start FDC2214 with 4 channels init
  	//bool capOk = capsense.begin(0xF, 0x4, 0x5); //setup all four channels, autoscan with 2 channels, deglitch at 10MHz 
  	if (capOk){
		Serial.println("Sensor OK");
    }
  	else{ 
		Serial.println("Sensor Fail");
    }
}

/**
 * @brief 用于检测ST25DV16 GPO引脚中断
 * 
 */

Button button1 = {ST25DV_GPO, 0, false};
void ARDUINO_ISR_ATTR isr(void* arg) {
    Button* s = static_cast<Button*>(arg);
    s->numberKeyPresses += 1;
    s->pressed = true;
}

uint16_t LastSamplePointer = SMARTAG_BEGIN_ADDR_COMPACT_DATA;
bool  nfc_write_notify = false; // NFC写入通知
SFE_ST25DV64KC_NDEF tag;
uint32_t RFActivityStatus = FIELD_UNDEF;
volatile uint32_t RFActivity = 0;
/**
 * 初始化ST25DV64, 使用Wire1
 */
void st25dv64_init(void)
{
	Wire1.begin(ST25DV_SDA, ST25DV_SCLK);
	if (!tag.begin(Wire1))
  	{
		Serial.println(F("ST25 not detected. Freezing..."));
		while(1)
		{
			//do something
		}
  	}
	//写入EH_MODE，启动后强制打开EH
	uint8_t data[1] = {0x00};
	tag.writeEEPROM(0x0002, data, 1);
	uint8_t data1[1] = {0x01};
	tag.writeEEPROM(0x2002, data1, 1);

    pinMode(button1.PIN, INPUT_PULLUP);
    attachInterruptArg(button1.PIN, isr, &button1, FALLING);
}

SPARKFUN_LIS2DH12 accel; // Create instance
/**
 * LIS2DH12初始化，使用Wire
 */
void lis2dh12_init(void)
{
	Serial.println("SparkFun Accel Example");
	Wire.begin(LIS2DH12_SDA, LIS2DH12_SCLK);

	if (accel.begin() == false)
	{
		Serial.println("Accelerometer not detected. Check address jumper and wiring. Freezing...");
		while (1);
	}
}

MAX30105 _particleSensor;
SoftWire _Wire_MAX30101;
void init_max30101()
{
  _Wire_MAX30101.begin(MAX30101_SDA_PIN, MAX30101_SCL_PIN);
  // Initialize sensor
  if (_particleSensor.begin(_Wire_MAX30101, I2C_SPEED_STANDARD) == false)
  {
    Serial.println("MAX30105 was not found. Please check wiring/power. ");
    while (1);
  }
  byte ledBrightness = 10;//Options: 0=Off to 255=50mA
  byte sampleAverage = 1; //Options: 1, 2, 4, 8, 16, 32
  byte ledMode = 2; //Options: 1 = Red only, 2 = Red + IR, 3 = Red + IR + Green
  int sampleRate = 100; //Options: 50, 100, 200, 400, 800, 1000, 1600, 3200
  int pulseWidth = 69; //Options: 69, 118, 215, 411
  int adcRange = 4096; //Options: 2048, 4096, 8192, 16384

  _particleSensor.setup(ledBrightness, sampleAverage, ledMode, sampleRate, pulseWidth, adcRange); //Configure sensor with these settings
}

float readADC_Avg(void) {
  // 读取ADC值
  int adcValue = analogRead(36);
  
  // 转换为电压值 (假设3.3V参考电压)
  float voltage = adcValue * (4.0 / 4095.0) * 4; // 4倍增益
  return voltage; // 返回电压值
}
